﻿#include "formula.h"

Formula::Formula(QObject *parent) : QObject(parent)
{
    formulaBase = new FormulaBase();
}

Formula::Formula(FormulaBase *formulaBase){
    this->formulaBase = formulaBase;

    combineProduct = helper.getProductById(formulaBase->combineId);
    combineProduct->count = formulaBase->combineCount;

    splitProducts.clear();
    QStringList splitIds = formulaBase->splitIds.split("#");
    QStringList splitCounts = formulaBase->splitCounts.split("#");
    for(int i=0;i<splitIds.size();i++){
        Product *product = helper.getProductById(splitIds[i]);
        if(product==nullptr){
            break;
        }
        product->count = splitCounts[i].toInt();
        splitProducts.append(product);
    }
}

void Formula::setCombineProduct(Product *product){
    formulaBase->companyId = product->companyId;
    formulaBase->combineId = product->productId;
    formulaBase->combineCount = product->count;

    combineProduct = product;
}

Product* Formula::getCombineProduct(){
    return combineProduct;
}

void Formula::setSplitProducts(QList<Product*> products){
    formulaBase->splitIds.clear();
    formulaBase->splitCounts.clear();

    for(Product *product:products){
        formulaBase->splitIds.append(product->productId);
        formulaBase->splitIds.append("#");
        formulaBase->splitCounts.append(QString("%1").arg(product->count));
        formulaBase->splitCounts.append("#");
    }

    splitProducts = products;
}

QList<Product*> Formula::getSplitProducts(){
    return splitProducts;
}
